﻿using System;
using FBGraph.Controllers.Connections.Events;
using FBGraph.Models;

namespace FBGraph.Controllers
{
    public partial class EventsController : IFBGraphPrimaryController<Event>, IFBGraphSearchController<Event>
    {
        /// <summary>Initializes subcontrollers contained by <see cref="EventsController" />.</summary>
        protected override void InitSubcontrollers()
        {
            this.Attending = new AttendingController(Context);
            this.Declined = new DeclinedController(Context);
            this.Invited = new InvitedController(Context);
            this.Maybe = new MaybeController(Context);
            this.NoReply = new NoreplyController(Context);
        }

        /// <summary>Gets a reference to a <see cref="AttendingController" />, which provides access to the attending connection for events.</summary>
        public AttendingController Attending { get; private set; }

        /// <summary>Gets a reference to a <see cref="DeclinedController" />, which provides access to the declined connection for events.</summary>
        public DeclinedController Declined { get; private set; }

        /// <summary>Gets a reference to a <see cref="InvitedController" />, which provides access to the invited connection for events.</summary>
        public InvitedController Invited { get; private set; }

        /// <summary>Gets a reference to a <see cref="MaybeController" />, which provides access to the maybe connection for events.</summary>
        public MaybeController Maybe { get; private set; }

        /// <summary>Gets a reference to a <see cref="NoreplyController" />, which provides access to the noreply connection for events.</summary>
        public NoreplyController NoReply { get; private set; }        

        /// <summary>Creates an event for the specified profile.</summary>
        /// <param name="profileId">The Graph API identifier of the profile for which the event will be created. This may be an identifier for a <see cref="User" /> or <see cref="Page" />.</param>
        /// <param name="name">The name of the event.</param>
        /// <param name="startTime">The start date and time of the event.</param>
        /// <param name="endTime">The end date and time of the event.</param>
        /// <returns>The identifier of the created event.</returns>
        public Object Create(Object profileId, String name, DateTime startTime, DateTime endTime)
        {
            var args = new FBArgs
            {
                { "name", name },
                { "start_time", startTime.ToUniversalTime().ToUnixDateTime().Value },
                { "end_time", endTime.ToUniversalTime().ToUnixDateTime().Value }
            };

            var result = Context.Post(profileId, "events", args);

            return result.Id;
        }

        /// <summary>Responds to an event invitation on behalf of the currently logged in user.</summary>
        /// <param name="eventId">The Graph API identifier for the event.</param>
        /// <param name="rsvp">The type of response.</param>
        /// <returns>The identifier of the event that was responded to.</returns>
        public Object Rsvp(Object eventId, EventRsvpType rsvp)
        {
            var result = Context.Post(eventId, rsvp.ToString().ToLower(), null);
            return result.Id;
        }

        #region [ IFBGraphSearchController<Event> Members ]

        String IFBGraphSearchController<Event>.SearchType
        {
            get { return "event"; }
        }

        #endregion
    }
}